/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workers.statements;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
import au.com.bytecode.opencsv.bean.CsvToBean;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.faces.model.DataModel;
import javax.persistence.Query;
import entities.Merchants;
import session.MerchantsFacade;
import controllers.MerchantsController;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.PersistenceContext;
import workers.MerchantsWorker;
import workers.MerchantsWorkerImpl;
import workers.statements.beans.CCStatementCSV;

/**
 *
 * @author bsmith
 */
//@ManagedBean(name = "StatementLoader")
//@SessionScoped
@Stateless
@Local
public class StatementLoaderImpl implements StatementLoader {

    private List<InputStream> dataInputStreams;
    private List<CCStatementCSV> dataList;
    @EJB
    private MerchantsFacade ejbMerchantsFacade;
    @EJB
    private MerchantsWorker merchantsWorker;

    public StatementLoaderImpl() {
        //merchantsWorker = new MerchantsWorker(merchantsFacade);
    }

    public StatementLoaderImpl(List<InputStream> dataInputStreams) throws IOException {
        this.dataInputStreams = dataInputStreams;
        //this.ejbMerchantsFacade = merchantsFacade;
        //merchantsWorker = new MerchantsWorker(merchantsFacade);
        parseData();
        printData();
    }

    public StatementLoaderImpl(List<InputStream> dataInputStreams, MerchantsFacade merchantsFacade) throws IOException {
        this.dataInputStreams = dataInputStreams;
        this.ejbMerchantsFacade = merchantsFacade;
        //merchantsWorker = new MerchantsWorker(merchantsFacade);
        parseData();
        printData();
    }

    public void load(List<InputStream> dataInputStreams) throws IOException {
        this.dataInputStreams = dataInputStreams;
        //this.ejbMerchantsFacade = merchantsFacade;
        //merchantsWorker = new MerchantsWorker(merchantsFacade);
        parseData();
        printData();
    }
    
    public static List<InputStream> getSampleData() {
        List<InputStream> list = new ArrayList<InputStream>();
        final String resourcesPath = "resources/sampledataBIG02.csv";//sampledata03.csv";//sampledataBIG02.csv//sampledata01.csv
        InputStream stream = StatementLoaderImpl.class.getResourceAsStream(resourcesPath);
        System.out.println("Stream is: " + stream.toString());
        list.add(stream);
        return list;
    }

    private void parseData() throws IOException {
        InputStream is = dataInputStreams.get(0);
        CSVReader reader = new CSVReader(new InputStreamReader(is));
        ColumnPositionMappingStrategy<CCStatementCSV> strat = new ColumnPositionMappingStrategy<CCStatementCSV>();
        strat.setType(CCStatementCSV.class);
        String[] columns = new String[]{"Date", "Amount", "Id", "Unknown", "Type", "Merchant", "Balance"}; // the fields to bind do in your JavaBean
        strat.setColumnMapping(columns);

        String[] line;

        CsvToBean<CCStatementCSV> csv = new CsvToBean<CCStatementCSV>();
        this.dataList = csv.parse(strat, reader);
    }

    public List<CCStatementCSV> getDataList() {
        return this.dataList;
    }

    private void printData() {
        for (Object item : this.dataList) {
            CCStatementCSV ccs = (CCStatementCSV) item;
            System.out.println("  CCStatement:" + ccs.toString());
        }
    }
}
